// 封装ajax
// 功能：前后端交互
// 参数：
    // 请求方式:method
    // 请求地址:url
    // 是否异步:isAsync
    // 请求参数（浏览器端发给服务器端的数据）:params
    // 回调函数:callback
// 返回值：无

// let cb = function(str){
//     document.getElementById("msg").innerHTML = str;
// }
// ajax2013("get","regSave.php","username=jzm&userpass=123",cb, true);

function ajax2013(method,url,params,callback,isAsync){
    
    let xhr =new XMLHttpRequest();

    let urlAndParms = url;  //regSave.php
    if(method.toLowerCase()=="get"){
        urlAndParms += "?"+params ;//regSave.php?username=jzm&userpass=123
    }

    xhr.open(method,urlAndParms,isAsync);

    xhr.onreadystatechange = function(){
        if(xhr.readyState==4 && xhr.status==200){
            callback(xhr.responseText);            
        }
    }

    if(method.toLowerCase()=="get"){
        xhr.send();
    }else{ //post
        xhr.setRequestHeader("content-type","application/x-www-form-urlencoded");
        xhr.send(params);
    }

}


// ajax2013useobj({
//     url:"regSave.php",
//     params:"username=jzm&userpass=123",
//     callback:function(str){}
// })


function ajax2013useobj(obj){
    // 默认值：
    let defaultObj = {
        method:"get",
        url:"#",
        params:"",
        isAsync:true,    
        callback:null
    };

    for(let key in defaultObj){
        if(!obj[key]){
            obj[key] = defaultObj[key];
        }
    }


    let xhr =new XMLHttpRequest();

    let urlAndParms = obj.url;  //regSave.php
    if(obj.method.toLowerCase()=="get"){
        urlAndParms += "?"+obj.params ;//regSave.php?username=jzm&userpass=123
    }

    xhr.open(obj.method,urlAndParms,obj.isAsync);

    xhr.onreadystatechange = function(){
        if(xhr.readyState==4 && xhr.status==200){
            obj.callback(xhr.responseText);            
        }
    }

    if(obj.method.toLowerCase()=="get"){
        xhr.send();
    }else{ //post
        xhr.setRequestHeader("content-type","application/x-www-form-urlencoded");
        xhr.send(obj.params);
    }

}


function ajax2013useobjandpromise(obj){
    // 默认值：
    let defaultObj = {
        method:"get",
        url:"#",
        params:"",
        isAsync:true
    };

    for(let key in defaultObj){
        if(!obj[key]){
            obj[key] = defaultObj[key];
        }
    }

    let xhr =new XMLHttpRequest();

    let urlAndParms = obj.url;  //regSave.php
    if(obj.method.toLowerCase()=="get"){
        urlAndParms += "?"+obj.params ;//regSave.php?username=jzm&userpass=123
    }
    xhr.open(obj.method,urlAndParms,obj.isAsync);

    let p = new Promise(function(resolve,reject){
        xhr.onreadystatechange = function(){  
            if(xhr.readyState==4){
                if(xhr.status==200){
                    resolve(xhr.responseText);
                }else{
                    reject("服务器错了");
                }
            }
        }  
    });

    if(obj.method.toLowerCase()=="get"){
        xhr.send();
    }else{ //post
        xhr.setRequestHeader("content-type","application/x-www-form-urlencoded");
        xhr.send(obj.params);
    }

    return p;
}